package com.jade.file.service.impl;

import java.io.File;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.List;

import com.jade.common.utils.DateUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.jade.file.mapper.TFileUploadMapper;
import com.jade.file.domain.TFileUpload;
import com.jade.file.service.ITFileUploadService;
import com.jade.common.core.text.Convert;

/**
 * 文件模块Service业务层处理
 * 
 * @author jade
 * @date 2020-05-09
 */
@Service
public class TFileUploadServiceImpl implements ITFileUploadService 
{
    @Autowired
    private TFileUploadMapper tFileUploadMapper;

    @Value("${file_uploadPath}")
    private String uploadPath;
    
    /**
     * 查询文件模块
     * 
     * @param fileId 文件模块ID
     * @return 文件模块
     */
    @Override
    public TFileUpload selectTFileUploadById(Long fileId)
    {
        return tFileUploadMapper.selectTFileUploadById(fileId);
    }

    /**
     * 查询文件模块列表
     * 
     * @param tFileUpload 文件模块
     * @return 文件模块
     */
    @Override
    public List<TFileUpload> selectTFileUploadList(TFileUpload tFileUpload)
    {
        return tFileUploadMapper.selectTFileUploadList(tFileUpload);
    }

    /**
     * 新增文件模块
     * 
     * @param tFileUpload 文件模块
     * @return 结果
     */
    @Override
    public int insertTFileUpload(TFileUpload tFileUpload)
    {
        tFileUpload.setCreateTime(DateUtils.getNowDate());
        return tFileUploadMapper.insertTFileUpload(tFileUpload);
    }

    /**
     * 修改文件模块
     * 
     * @param tFileUpload 文件模块
     * @return 结果
     */
    @Override
    public int updateTFileUpload(TFileUpload tFileUpload)
    {
        tFileUpload.setUpdateTime(DateUtils.getNowDate());
        return tFileUploadMapper.updateTFileUpload(tFileUpload);
    }

    /**
     * 删除文件模块对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTFileUploadByIds(String ids)
    {
        return tFileUploadMapper.deleteTFileUploadByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除文件模块信息
     * 
     * @param fileId 文件模块ID
     * @return 结果
     */
    public int deleteTFileUploadById(Long fileId)
    {
        return tFileUploadMapper.deleteTFileUploadById(fileId);
    }

	private  String getFolder() {
    	Calendar ca=Calendar.getInstance();
    	int year = ca.get(1);
    	int month = ca.get(2);
    	int date = ca.get(5);
		return year+month+date+"";
	}

	/**
	 * 创建文件
	 * 理论上来说，创建文件失败应该通知上层知道，所以抛出异常
	 */
	@Override
	public TFileUpload makeFile(String fileData, String fileName) throws Exception {
		 FileWriter fw = null;     
		try {
				String dirPath = uploadPath+"/"+getFolder();
				 File FilePath = new File(dirPath);
				 if (!FilePath.exists()) {
				 	FilePath.mkdirs();
				}
				 fw =new FileWriter(FilePath);
				 fw.write(fileData);
				 fw.flush();
				 
				 TFileUpload tfu=new TFileUpload();
	             tfu.setFileName(fileName);
	             tfu.setFilePath(dirPath);
	             this.insertTFileUpload(tfu);
	             return tfu;
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception(e);
			}finally{
				 fw.close();
			} 
	}
}
